home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWGraphx / Sources / FWPoly.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  11.5 KB  |  397 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWPoly.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWPOLY_H
  13. #include "FWPoly.h"
  14. #endif
  15.  
  16. #ifndef FWPOINT_H
  17. #include "FWPoint.h"
  18. #endif
  19.  
  20. #ifndef FWRECT_H
  21. #include "FWRect.h"
  22. #endif
  23.  
  24. // ----- Foundation Includes -----
  25.  
  26. #ifndef FWSTREAM_H
  27. #include "FWStream.h"
  28. #endif
  29.  
  30. #ifndef FWMEMORY_H
  31. #include "FWMemory.h"
  32. #endif
  33.  
  34. // ----- OpenDoc Includes -----
  35.  
  36. #ifndef _TRANSFORM_
  37. #include <Trnsform.xh>
  38. #endif
  39.  
  40. //========================================================================================
  41. //    RunTime Info
  42. //========================================================================================
  43.  
  44. #if FW_LIB_EXPORT_PRAGMAS
  45. #pragma lib_export on
  46. #endif
  47.  
  48. #ifdef FW_BUILD_MAC
  49. #pragma segment FWGraphics_Polygon
  50. #endif
  51.  
  52. FW_DEFINE_CLASS_M1(FW_PPolygon, FW_CGraphicCountedPtr)
  53. FW_DEFINE_CLASS_M1(FW_CPolygonRep, FW_CGraphicCountedPtrRep)
  54.  
  55. FW_REGISTER_ARCHIVABLE_CLASS(FW_LPolygonRep, FW_CPolygonRep, FW_CPolygonRep::Read, FW_CGraphicCountedPtrRep::Write)
  56.  
  57. //========================================================================================
  58. //    class FW_PPolygon
  59. //========================================================================================
  60.  
  61. //----------------------------------------------------------------------------------------
  62. //    FW_PPolygon::FW_PPolygon
  63. //----------------------------------------------------------------------------------------
  64.  
  65. FW_PPolygon::FW_PPolygon() :
  66.     FW_CGraphicCountedPtr()
  67. {
  68. }
  69.  
  70. //----------------------------------------------------------------------------------------
  71. //    FW_PPolygon::FW_PPolygon
  72. //----------------------------------------------------------------------------------------
  73.  
  74. FW_PPolygon::FW_PPolygon(FW_CReadableStream& archive) :
  75.     FW_CGraphicCountedPtr()
  76. {
  77.     SetRep(new FW_CPolygonRep(archive));
  78. }
  79.  
  80. //----------------------------------------------------------------------------------------
  81. //    FW_PPolygon::FW_PPolygon
  82. //----------------------------------------------------------------------------------------
  83.  
  84. FW_PPolygon::FW_PPolygon(unsigned long count, const FW_CPoint* points) :
  85.     FW_CGraphicCountedPtr()
  86. {
  87.     SetRep(new FW_CPolygonRep(count, points));
  88. }
  89.  
  90. //----------------------------------------------------------------------------------------
  91. //    FW_PPolygon::FW_PPolygon
  92. //----------------------------------------------------------------------------------------
  93.  
  94. FW_PPolygon::FW_PPolygon(const FW_PPolygon& other) :
  95.     FW_CGraphicCountedPtr(other)
  96. {
  97. }
  98.  
  99. //----------------------------------------------------------------------------------------
  100. //    FW_PPolygon::FW_PPolygon
  101. //----------------------------------------------------------------------------------------
  102.  
  103. FW_PPolygon::~FW_PPolygon()
  104. {
  105. }
  106.  
  107. //----------------------------------------------------------------------------------------
  108. //    FW_PPolygon::operator=
  109. //----------------------------------------------------------------------------------------
  110.  
  111. FW_PPolygon& FW_PPolygon::operator=(const FW_PPolygon& other)
  112. {
  113.     // We don't need to test this == &other because SetRep will do it
  114.     SetRep(other.GetRep());
  115.     return *this;
  116. }
  117.  
  118. //========================================================================================
  119. //    class FW_CPolygonRep
  120. //========================================================================================
  121.  
  122. //----------------------------------------------------------------------------------------
  123. //    FW_CPolygonRep::FW_CPolygonRep
  124. //----------------------------------------------------------------------------------------
  125.  
  126. FW_CPolygonRep::FW_CPolygonRep(unsigned long count, const FW_CPoint* points):
  127.     fCount(count),
  128.     fPoints(NULL)
  129. {
  130.     FW_ASSERT(fCount != 0);
  131.     FW_ASSERT(points != NULL);
  132.     
  133.     unsigned long memorySize = count * sizeof(FW_CPoint);    
  134.     fPoints = (FW_CPoint*)FW_CMemoryManager::AllocateBlock(memorySize);
  135.     
  136.     FW_CMemoryManager::CopyMemory(points, fPoints, memorySize);
  137. }
  138.  
  139. //----------------------------------------------------------------------------------------
  140. //    FW_CPolygonRep::FW_CPolygonRep
  141. //----------------------------------------------------------------------------------------
  142.  
  143. FW_CPolygonRep::FW_CPolygonRep(FW_CReadableStream& archive):
  144.     fCount(0),
  145.     fPoints(NULL)
  146. {
  147.     archive >> fCount;
  148.     
  149.     unsigned long memorySize = fCount * sizeof(FW_CPoint);    
  150.     fPoints = (FW_CPoint*)FW_CMemoryManager::AllocateBlock(memorySize);
  151.     
  152.     for (unsigned long i = 0; i< fCount; i++)
  153.         archive >> fPoints[i];
  154. }
  155.  
  156. //----------------------------------------------------------------------------------------
  157. //    FW_CPolygonRep::~FW_CPolygonRep
  158. //----------------------------------------------------------------------------------------
  159.  
  160. FW_CPolygonRep::~FW_CPolygonRep()
  161. {
  162.     FW_CMemoryManager::FreeBlock(fPoints);    // FreeBlock tests for NULL
  163. }
  164.  
  165. //----------------------------------------------------------------------------------------
  166. //    FW_CPolygonRep::Transform
  167. //----------------------------------------------------------------------------------------
  168.  
  169. void FW_CPolygonRep::Transform(Environment *ev, ODTransform* transform)
  170. {
  171.     for(unsigned long i = 0; i < fCount; i++)
  172.         fPoints[i].Transform(ev, transform);
  173. }
  174.  
  175. //----------------------------------------------------------------------------------------
  176. //    FW_CPolygonRep::InverseTransform
  177. //----------------------------------------------------------------------------------------
  178.  
  179. void FW_CPolygonRep::InverseTransform(Environment *ev, ODTransform* transform)
  180. {
  181.     for(unsigned long i = 0; i < fCount; i++)
  182.         fPoints[i].InverseTransform(ev, transform);
  183. }
  184.  
  185. //----------------------------------------------------------------------------------------
  186. //    FW_CPolygonRep::Move
  187. //----------------------------------------------------------------------------------------
  188.  
  189. void FW_CPolygonRep::Move(FW_CFixed deltaX, FW_CFixed deltaY)
  190. {
  191.     for(unsigned long i = 0; i < fCount; i++)
  192.     {
  193.         fPoints[i].x += deltaX;
  194.         fPoints[i].y += deltaY;
  195.     }
  196. }
  197.  
  198. //----------------------------------------------------------------------------------------
  199. //    FW_CPolygonRep::MoveTo
  200. //----------------------------------------------------------------------------------------
  201.  
  202. void FW_CPolygonRep::MoveTo(FW_CFixed x, FW_CFixed y)
  203. {
  204.     FW_CRect bounds;
  205.     GetBounds(bounds);
  206.     
  207.     FW_CFixed deltaX = x - bounds.left;
  208.     FW_CFixed deltaY = y - bounds.top;
  209.     
  210.     Move(deltaX, deltaY);
  211. }
  212.  
  213. //----------------------------------------------------------------------------------------
  214. //    FW_CPolygonRep::Inset
  215. //----------------------------------------------------------------------------------------
  216.  
  217. void FW_CPolygonRep::Inset(FW_CFixed x, FW_CFixed y)
  218. {
  219.     // Find the geometric center of the polygon
  220.     FW_CWide xCenterWide = FW_IntToWide(0);
  221.     FW_CWide yCenterWide = xCenterWide;
  222.     
  223.     unsigned long i;
  224.     for(i = 0; i < fCount; i ++)
  225.     {
  226.         xCenterWide += FW_CWide(fPoints[i].x);
  227.         yCenterWide += FW_CWide(fPoints[i].y);
  228.     }
  229.     
  230.     FW_CFixed xCenter = xCenterWide / FW_IntToFixed(fCount);
  231.     FW_CFixed yCenter = yCenterWide / FW_IntToFixed(fCount);
  232.  
  233.     // Now offset all points
  234.     for(i = 0; i < fCount; i ++)
  235.     {
  236.         if(fPoints[i].x < xCenter)
  237.         {
  238.             // Offset the coordinate
  239.             fPoints[i].x += x;
  240.             
  241.             // Check for carryover
  242.             if(fPoints[i].x > xCenter)
  243.                 fPoints[i].x = xCenter;
  244.         }
  245.         else if(fPoints[i].x > xCenter)
  246.         {
  247.             // Offset the coordinate
  248.             fPoints[i].x -= x;
  249.             
  250.             // Check for carryover
  251.             if(fPoints[i].x < xCenter)
  252.                 fPoints[i].x = xCenter;
  253.         }
  254.  
  255.         if(fPoints[i].y < yCenter)
  256.         {
  257.             // Offset the coordinate
  258.             fPoints[i].y += y;
  259.             
  260.             // Check for carryover
  261.             if(fPoints[i].y > yCenter)
  262.                 fPoints[i].y = yCenter;
  263.         }
  264.         else if(fPoints[i].y > yCenter)
  265.         {
  266.             // Offset the coordinate
  267.             fPoints[i].y -= y;
  268.  
  269.             // Check for carryover
  270.             if(fPoints[i].y < yCenter)
  271.                 fPoints[i].y = yCenter;
  272.         }
  273.     }
  274. }
  275.  
  276. //----------------------------------------------------------------------------------------
  277. //    FW_CPolygonRep::GetBounds
  278. //----------------------------------------------------------------------------------------
  279.  
  280. void FW_CPolygonRep::GetBounds(FW_CRect& rect) const
  281. {
  282.     rect.TopLeft() = fPoints[0];
  283.     rect.BotRight() = fPoints[0];
  284.     
  285.     for(unsigned long i = 1; i < fCount; i ++)
  286.     {
  287.         if(fPoints[i].x < rect.left)
  288.             rect.left = fPoints[i].x;
  289.         else if(fPoints[i].x > rect.right)
  290.             rect.right = fPoints[i].x;
  291.  
  292.         if(fPoints[i].y < rect.top)
  293.             rect.top = fPoints[i].y;
  294.         else if(fPoints[i].y > rect.bottom)
  295.             rect.bottom = fPoints[i].y;
  296.     }
  297. }
  298.  
  299. //----------------------------------------------------------------------------------------
  300. //    FW_CPolygonRep::SetPoints
  301. //----------------------------------------------------------------------------------------
  302.  
  303. void FW_CPolygonRep::SetPoints(const FW_CPoint* points,
  304.                                unsigned long startIndex,
  305.                                unsigned long pointCount)
  306. {
  307.     FW_ASSERT(startIndex + pointCount <= fCount);
  308.     FW_CMemoryManager::CopyMemory(points, fPoints + startIndex, pointCount * sizeof(FW_CPoint));
  309. }
  310.  
  311. //----------------------------------------------------------------------------------------
  312. //    FW_CPolygonRep::GetPoints
  313. //----------------------------------------------------------------------------------------
  314.  
  315. void FW_CPolygonRep::GetPoints(FW_CPoint* points,
  316.                                unsigned long startIndex,
  317.                                unsigned long pointCount) const
  318. {
  319.     FW_ASSERT(startIndex + pointCount <= fCount);
  320.     FW_CMemoryManager::CopyMemory(fPoints + startIndex, points, pointCount * sizeof(FW_CPoint));
  321. }
  322.  
  323. //----------------------------------------------------------------------------------------
  324. //    FW_CPolygonRep::GetCount
  325. //----------------------------------------------------------------------------------------
  326.  
  327. unsigned long FW_CPolygonRep::GetCount() const
  328. {
  329.     return fCount;
  330. }
  331.  
  332. //----------------------------------------------------------------------------------------
  333. //    FW_CPolygonRep::GetPoints
  334. //----------------------------------------------------------------------------------------
  335.  
  336. const FW_CPoint* FW_CPolygonRep::GetPoints() const
  337. {
  338.     return fPoints;
  339. }
  340.  
  341. //----------------------------------------------------------------------------------------
  342. //    FW_CPolygonRep::Flatten
  343. //----------------------------------------------------------------------------------------
  344.  
  345. void FW_CPolygonRep::Flatten(FW_CWritableStream& archive) const
  346. {
  347.     archive << fCount;
  348.     for (unsigned long i = 0; i< fCount; i++)
  349.         archive << fPoints[i];
  350. }
  351.  
  352. //----------------------------------------------------------------------------------------
  353. //    FW_CPolygonRep::Copy
  354. //----------------------------------------------------------------------------------------
  355.  
  356. FW_PPolygon FW_CPolygonRep::Copy() const
  357. {
  358.     return FW_PPolygon(fCount, fPoints);;
  359. }
  360.  
  361. //----------------------------------------------------------------------------------------
  362. //    FW_CPolygonRep::IsEqual
  363. //----------------------------------------------------------------------------------------
  364.  
  365. FW_Boolean FW_CPolygonRep::IsEqual(const FW_CGraphicCountedPtrRep* other) const
  366. {
  367.     if (other == this)
  368.         return TRUE;
  369.         
  370.     FW_CPolygonRep* polygonRep = FW_DYNAMIC_CAST(FW_CPolygonRep, other);
  371.     if (polygonRep != NULL)
  372.     {
  373.         if(fCount == polygonRep->fCount)
  374.         {
  375.             for (unsigned long i = 0; i< fCount; i++)
  376.             {
  377.                 if (polygonRep->fPoints[i] != fPoints[i])
  378.                     return FALSE;
  379.             }
  380.             return TRUE;
  381.         }
  382.     }
  383.     
  384.     return FALSE;
  385. }
  386.  
  387. //----------------------------------------------------------------------------------------
  388. //    FW_CPolygonRep::Read
  389. //----------------------------------------------------------------------------------------
  390.  
  391. void* FW_CPolygonRep::Read(FW_CReadableStream& archive)
  392. {
  393.     FW_CPolygonRep* objectRep = new FW_CPolygonRep(archive);
  394.     return objectRep;
  395. }
  396.  
  397.